package com.javathread.th7;

import com.javathread.thread01.SleepTools;

public class NormalDeadLock {
   private static Object valueFirst = new Object(); //第一个锁
   private static Object valueSecond = new Object(); //第二个锁
   
   //先拿第一个锁  再拿第二个锁
   private static void firstToSecond() throws InterruptedException {
	   String threadName = Thread.currentThread().getName();
	   synchronized (valueFirst) {
		   System.out.println(threadName + " get first");
		   SleepTools.ms(100);
		   synchronized (valueSecond) {
			   System.out.println(threadName + " get second");
		   }
	   }
   }
   
   private static void SecondToFirst() throws InterruptedException {
	   String threadName = Thread.currentThread().getName();
	   synchronized (valueSecond) {
		   System.out.println(threadName + " get second");
		   SleepTools.ms(100);
		   synchronized (valueFirst) {
			   System.out.println(threadName + " get first");
		   }
	   }
   }
   
   private static class TestThread extends Thread{
	   private String name;
	   public TestThread(String name) {
		   this.name = name;
	   }
	   
	   public void run() {
		   Thread.currentThread().setName(name);
		   try {
			   SecondToFirst();
		   } catch (InterruptedException e) {
			   e.printStackTrace();
		   }
	   }
   }
   
   public static void main(String[] args) {
	   Thread.currentThread().setName("TestDeadLock");
	   TestThread testThread = new TestThread("subTestThread");
	   testThread.start();
	   try {
		firstToSecond();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
   }
}
